from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from ctypes import *


m_hScanner = (c_int*1)(0);
myDll = CDLL("D:\\djangoproject\\rfid\\rfid\VHLib-64.dll")
def index(request):
	return render(request,"index.html")

#连接读卡器
def connectUSB(reqeust):

	myDll.VH_ConnectScannerUsb.argtypes=[POINTER(c_int)]
	myDll.VH_ConnectScannerUsb.restype=c_short
	res = c_short(0)
	res = myDll.VH_ConnectScannerUsb(m_hScanner)
	lopt = c_long(3); #3表示接口位USB
	myDll.VH_SetCommunicationOpt.argtypes=[c_long]
	myDll.VH_SetCommunicationOpt(lopt, None)

	HardVersion = (c_short*1)(0)
	SoftVersion = (c_short*1)(0)
	myDll.VH_GetVersion(m_hScanner[0], HardVersion, SoftVersion)


	myDll.EPC1G2_EasAlarm.argtypes=[c_int,c_int]
	myDll.EPC1G2_EasAlarm.restype=c_short
	myDll.EPC1G2_EasAlarm(m_hScanner[0],1)
	return HttpResponse(res)

#断开读卡器
def disConnectReader(request):
	print(m_hScanner[0]);
	myDll.DisconnectScanner.argtypes=[c_int]
	myDll.DisconnectScanner.restype=c_short
	res = myDll.DisconnectScanner(m_hScanner[0]);
	print(res);
	return HttpResponse(res);

# public short EPC1G2_ReadWordBlock(int hScanner, int EPC_WORD,
# 				byte[] IDBuffer, int mem, int ptr, int len, byte[] Data,
# 				byte[] intAccessPassword, int Address);

def readUserArea(request):
	myDll.EPC1G2_ReadWordBlock.argtypes=[c_int,c_int,POINTER(c_byte),c_int,c_int,c_int,POINTER(c_byte),POINTER(c_byte),c_int]
	myDll.EPC1G2_ReadWordBlock.restype = c_short
	return HttpResponse("test");